package com.example.lxq.demo.gui;


import java.io.File;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MusicJFrame extends JFrame{
    
    private JLabel musicRootPath;
    private JButton choice;
    private JList musicList;
    //private ArrayList musicArray;
    public static void main(String[] args) {
        new MusicJFrame();
    }
    private void initView(){
        musicRootPath=new JLabel("请选择路径");
        choice=new JButton("选择路径");
        musicList=new JList(new String[]{"fjej"});
        musicList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //musicArray=new ArrayList<String>();

        addTogether();
        resizeTogether();
        registerListener();
    }

    private void registerListener(){
        choice.addActionListener((event)->{
            JFileChooser chooser=new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            int code=chooser.showDialog(new JLabel(), "选择项目文件夹");
            if(code==JFileChooser.APPROVE_OPTION){
                musicRootPath.setText(chooser.getSelectedFile().getAbsolutePath());
                File selectedFile=new File(chooser.getSelectedFile().getAbsolutePath());
                if(selectedFile.isDirectory()){
                    File files[]=selectedFile.listFiles();
                    for(File fileObj:files){
                        //musicArray.add(fileObj.getAbsoluteFile().toString());
                        musicList.add(fileObj.getAbsoluteFile().toString(),null);
                    }
                }else{
                    //musicArray.add(selectedFile.getAbsoluteFile().toString());
                    musicList.add(selectedFile.getAbsoluteFile().toString(),null);
                }
                
            }
        });
    }
    private void addTogether(){
        this.add(musicRootPath);
        this.add(choice);
        this.add(musicList);
    }
    private void resizeTogether(){
        configChoice();
        configRootPathView();
        musicList.setBounds((int)(getWidth()*0.6),choice.getHeight()+20,(int)(getWidth()*0.4),getHeight()-choice.getHeight()+20);
    }
    public MusicJFrame(){
        initView();
        setTitle("Java Music");
        setSize(800,800);
        setLayout(null);
        try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
				| UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        setLocationRelativeTo(getOwner());
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void configRootPathView(){
        musicRootPath.setBounds(0,20,(int)(800.0*0.7),50);
    }
    private void configChoice(){
        choice.setBounds((int)(800.0*0.7),20,(int)(800.0*0.3),50);
    }
}
